package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.DeviceCategoryMapper;
import com.yunhai.device.mapper.DeviceLockNetworkMapper;
import com.yunhai.device.service.DeviceLockNetworkService;
import com.yunhai.model.entity.dms.DmsDeviceCategoryEntity;
import com.yunhai.model.entity.dms.DmsDeviceLockNetworkEntity;
import com.yunhai.model.entity.dms.DmsDeviceMaintainEntity;
import com.yunhai.model.vo.dms.DmsDeviceLockNetworkQuery;
import com.yunhai.model.vo.dms.DmsDeviceLockNetworkVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DeviceLockNetworkServiceImpl implements DeviceLockNetworkService {

    @Resource
    private DeviceLockNetworkMapper deviceLockNetworkMapper;
    @Resource
    private DeviceCategoryMapper categoryMapper;

    /**
     * 获取 DeviceLockNetwork 分页列表
     */
    @Transactional
    public IPage getDeviceLockNetworkPagination(PaginationVo paginationVo, DmsDeviceLockNetworkQuery dmsDeviceLockNetworkQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsDeviceLockNetworkEntity> lqs = new LambdaQueryWrapper();

        lqs.eq(dmsDeviceLockNetworkQuery.getCategoryId()!=null,
                DmsDeviceLockNetworkEntity::getCategoryId,
                dmsDeviceLockNetworkQuery.getCategoryId()
        );
        lqs.orderByDesc( DmsDeviceLockNetworkEntity::getCreateTime );
        deviceLockNetworkMapper.selectPage(page, lqs);

        List<DmsDeviceLockNetworkEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        Set<Long> categoryIds = record.stream().map(item->item.getCategoryId()).collect(Collectors.toSet());
        if( !ObjectUtil.isNotEmptyList(categoryIds) ){
            return page;
        }

        List<DmsDeviceCategoryEntity> categoryList = categoryMapper.selectBatchIds(categoryIds);
        if( !ObjectUtil.isNotEmptyList(categoryList) ){
            return page;
        }

        for( DmsDeviceLockNetworkEntity detail : record ){
            categoryList.stream().filter(row->{
                return row.getId().equals(detail.getCategoryId());
            }).findFirst().ifPresent( row->{
                detail.setCategoryName(row.getName());
            });
        }

        return page;
    }


    /**
     * 获取 DeviceLockNetwork 所有数据
     *
     */
    @Transactional
    public List<DmsDeviceLockNetworkEntity> getDeviceLockNetworkAllList( DmsDeviceLockNetworkQuery dmsDeviceLockNetworkQuery ) throws Exception {

        LambdaQueryWrapper<DmsDeviceLockNetworkEntity> lqs = new LambdaQueryWrapper();
        List<DmsDeviceLockNetworkEntity> list = deviceLockNetworkMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 DeviceLockNetwork 详情
     */
    @Transactional
    public DmsDeviceLockNetworkEntity getDeviceLockNetworkDetail( Long id) throws Exception {

        DmsDeviceLockNetworkEntity dmsDeviceLockNetworkEntity = deviceLockNetworkMapper.selectById(id);
        return dmsDeviceLockNetworkEntity;
    }


    /**
     * 新增 DeviceLockNetwork
     * */
    @Transactional
    public void addDeviceLockNetwork(DmsDeviceLockNetworkVo dmsDeviceLockNetworkVo) throws Exception {

        DmsDeviceLockNetworkEntity dmsDeviceLockNetworkEntity = new DmsDeviceLockNetworkEntity();
        ReflectionCopy.copyProperties( dmsDeviceLockNetworkEntity, dmsDeviceLockNetworkVo );
        deviceLockNetworkMapper.insert(dmsDeviceLockNetworkEntity);
        return;
    }


    /**
     * 更新 DeviceLockNetwork
     * */
    @Transactional
    public void updateDeviceLockNetwork( DmsDeviceLockNetworkVo dmsDeviceLockNetworkVo) throws Exception {

        if( dmsDeviceLockNetworkVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsDeviceLockNetworkEntity dmsDeviceLockNetworkEntity = new DmsDeviceLockNetworkEntity();
        ReflectionCopy.copyProperties( dmsDeviceLockNetworkEntity, dmsDeviceLockNetworkVo );
        deviceLockNetworkMapper.updateById(dmsDeviceLockNetworkEntity);
        return;
    }

    /**
     * 删除 DeviceLockNetwork
     * */
    @Transactional
    public void batchRemoveDeviceLockNetwork( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        deviceLockNetworkMapper.deleteBatchIds(ids);
    }

}